package com.ctsi.canal.starter.config.configuration;

import com.ctsi.canal.starter.config.properties.CanalProperties;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author ajh
 */
@Configuration
@ConditionalOnProperty(value = CanalProperties.CANAL_ASYNC, havingValue = "true", matchIfMissing = true)
public class ThreadPoolConfiguration {


    @Bean(destroyMethod = "shutdown")
    public ExecutorService executorService() {

        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("canal-execute-thread-%d").build();
        return new ThreadPoolExecutor(10,
                20,
                200L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(),
                namedThreadFactory);
    }
}
